package me.gijspennings.jumpandduck.graphics;

import me.gijspennings.jumpandduck.entity.mob.Mob;
import me.gijspennings.jumpandduck.level.tiles.Tile;

public class Screen {

    private final int width, height;
    private int xOffset = 0, yOffset = 0;
    public int[] pixels;
    
    public Screen(int width, int height) {
        this.width = width;
        this.height = height;
        pixels = new int[width * height];
    }
    
    public void clear() {
        for (int i = 0; i < pixels.length; i++) pixels[i] = 0;
    }
    public void renderTile(Tile tile, int x, int y) {
        x -= xOffset;
        y -= yOffset;
        renderSprite(tile.getSprite(), x, y);
    }
    public void renderMob(Mob mob, int x, int y) {
        renderSprite(mob.getSprite(), x, y);
    }
    private void renderSprite(Sprite sprite, int x, int y) {
        for (int yy = 0; yy < sprite.getHeight(); yy++) {
            int ya = yy + y;
            if (ya < 0 || ya >= height) break;
            for (int xx = 0; xx < sprite.getWidth(); xx++) {
                int xa = xx + x;
                if (xa < -sprite.getWidth() || xa >= width) break;
                if (xa < 0) xa = 0;
                if (sprite.pixels[xx + yy * sprite.getWidth()] != Sprite.TRANSPARENT_COLOR) pixels[xa + ya * width] = sprite.pixels[xx + yy * sprite.getWidth()];
            }
        }
    }
    
    public void setOffset(int xOffset, int yOffset) {
        this.xOffset = xOffset;
        this.yOffset = yOffset;
    }
    
    public int getWidth() {
        return width;
    }
    public int getHeight() {
        return height;
    }
    public int getAmountOfHorizontalTiles() {
        return width / Sprite.DEFAULT_SIZE;
    }
    public int getAmountOfVerticalTiles() {
        return height / Sprite.DEFAULT_SIZE;
    }
}
